#include <omp.h>

#include "integrate0.h"
#include "common.h"
#include "common_cuda.h"

void integrate0(bodyList* bl, int iterations, int num_devices, float dt)
{
	#pragma omp parallel shared(bl) num_threads(num_devices)
	{
		int tid = omp_get_thread_num();

		int start_body = (bl->size*tid)/num_devices;
		int end_body = (bl->size*(tid+1))/num_devices;

		for (int i = 0; i != iterations; ++i)
		{
			for (int j = start_body; j != end_body; ++j)
			{
				float3 acc = make_float3(0.0f);
				for (int k = 0; k != bl->size; ++k)
					acc += calcGravityAccel(&bl->pos[j], &bl->pos[k]);

				bl->vel[j] += make_float4(dt*acc);
			}

			#pragma omp barrier

			for (int j = start_body; j != end_body; ++j)
				bl->pos[j] += dt*bl->vel[j];

			#pragma omp barrier
		}
	}
}
